﻿using System;
using System.Collections.Generic;

using System.Text;
using System.Xml;

namespace xReplace
{
    public abstract class AbstractProcessingCommand
    {

        public Content contentPipelines;

        private AbstractProcessingCommand()
        { }

        public AbstractProcessingCommand(ref Content pipes)
        {
            this.contentPipelines = pipes;        
        }

        abstract public bool ProcessCommand(XmlNodeList param);

        /// <summary>
        /// Provide the value of an XML node based on the key provided
        /// </summary>
        /// <param name="key">key to reference</param>
        /// <param name="nl">node list</param>
        /// <returns>value from within the XML</returns>
        public string GetNodeValue(string key, XmlNodeList nl) 
		{
			for(int i=0; i<nl.Count;i++)
			{
				if(nl[i].Name.ToLower() == key.ToLower())
				{
					return nl[i].InnerText;
				}
			}
			return "";
		}

        public bool GetNodeValueAsBoolean(string key, XmlNodeList nl)
        {
            string output = GetNodeValue(key, nl);
            return (output.ToLower() == "true") ? true : false;
        }

        /// <summary>
        /// Provide the value of an XML attribute/value based on the key and attribute name provided
        /// </summary>
        /// <param name="key">key to reference</param>
        /// <param name="attribute">attribute to reference</param>
        /// <param name="nl">node list</param>
        /// <returns>value from within the XML</returns>
        public string GetNodeAttribute(string key, string attribute, XmlNodeList nl)
        {
            for (int i = 0; i < nl.Count; i++)
            {
                if (nl[i].Name.ToLower() == key.ToLower())
                {
                    for (int j = 0; j < nl[i].Attributes.Count; j++)
                    {
                        if (nl[i].Attributes[j].Name.ToLower() == attribute.ToLower())
                        {
                            return nl[i].Attributes[j].Value;    
                        }
                    }
                }
            }
            return "";
        }

        public bool GetNodeAttributeAsBoolean(string key, string attribute, XmlNodeList nl)
        {
            string output = GetNodeAttribute(key,attribute, nl);
            return (output.ToLower() == "true") ? true : false;
        }

        /// <summary>
        /// Validate a node exists in a node list
        /// </summary>
        /// <param name="key">key to reference</param>
        /// <param name="nl">node list</param>
        /// <returns>true if the node exists in the node list</returns>
        public bool NodeExists(string key, XmlNodeList nl)
        {
            for (int i = 0; i < nl.Count; i++)
            {
                if (nl[i].Name.ToLower() == key.ToLower())
                {
                    return true;
                }
            }
            return false;
        }
    }
}
